import config, datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker,relationship

bind = create_engine(config.DB_URL)
Base = declarative_base(bind)
session = sessionmaker(bind)()


class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, autoincrement=True, primary_key=True, doc='主键')
    username = Column(String(64), unique=True, nullable=False, doc="用户名")
    nickname = Column(String(64), unique=True, nullable=False, doc="昵称")
    password = Column(String(32), nullable=False, doc="密码")
    avatar = Column(String(255), nullable=False, doc="头像")

    @property
    def status(self):
        '''
        请自己实现  这个项目用的redis比较多  我准备用redis实现
        :return:
        '''

        return True

    def keys(self):
        return (
            'id',
            'username',
            'nickname',
            'avatar',
        )

    def __getitem__(self, item):
        return getattr(self, item)


class Friends(Base):
    __tablename__ = 'friends'

    id = Column(Integer, autoincrement=True, primary_key=True, doc='主键')
    create_time = Column(TIMESTAMP, default=datetime.datetime.now, doc="创建时间")
    user_id = Column(ForeignKey(User.id,ondelete='CASCADE'))
    user = relationship(User)
